﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formProductRelocate : FormMDIChildTemplate
    {
        public formProductRelocate()
        {
            InitializeComponent();
        }

        private void formMaterialProductRelocate_Load(object sender, EventArgs e)
        { FillInvoice(); root.TranslateGrid(gridInvoice); }

        private void FillInvoice()
        {
            var invoiceRelocations = context.InvoiceRelocations.OrderByDescending(x => x.DateCreate);
            invoiceRelocationBindingSource.DataSource = invoiceRelocations;
            var storeHouses = context.StoreHouses.Select(x => x);
            storeHouseBindingSource.DataSource = storeHouses;
        }

        private void gridInvoice_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            string s = e.Value as string;
            StoreHouse storeHouse =
                (from x in storeHouseBindingSource.OfType<StoreHouse>()
                 where x.ToString() == s
                 select x).FirstOrDefault();
            e.Value = storeHouse;
            e.ParsingApplied = true;
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            this.ActiveControl = buttonSelect;
            root.Submit(); FillInvoice();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            InvoiceRelocation invoiceRelocation =
                (InvoiceRelocation)invoiceRelocationBindingSource.AddNew();
            if (gridInvoice.RowCount > 1)
                invoiceRelocation.InvoiceRelocationNumber =
                    context.InvoiceRelocations.Max(x => x.InvoiceRelocationNumber) + 1;
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (gridInvoice.Rows.Count > 0)
            {
                InvoiceRelocation invoiceRelocation = (InvoiceRelocation) invoiceRelocationBindingSource.Current;
                var formProductRelocateDetail = new formProductRelocateDetail(invoiceRelocation);

                if (invoiceRelocation.InvoiceIncome != null)
                {
                    formProductRelocateDetail.splitContainer.Panel1Collapsed = true;
                    formProductRelocateDetail.buttonRemove.Visible = false;
                }
                root.ShowForm(formProductRelocateDetail);
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            InvoiceRelocation invoiceRelocation = (InvoiceRelocation)invoiceRelocationBindingSource.Current;
            if(invoiceRelocation!=null )
                db.DeleteInvoiceRelocation(invoiceRelocation);
            invoiceRelocationBindingSource.Remove(invoiceRelocation);
            root.Submit();
        }

        private void invoiceRelocationBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            InvoiceRelocation invoiceRelocation = (InvoiceRelocation)invoiceRelocationBindingSource.Current;
            if (invoiceRelocation.InvoiceIncome == null)
            { gridInvoice.ReadOnly = false;  bindingNavigatorDeleteItem.Enabled = true; }
            else { gridInvoice.ReadOnly = true;  bindingNavigatorDeleteItem.Enabled = false; }

        }
    }
}
